package com.quik.web.front.service;

import java.text.SimpleDateFormat;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.quik.common.Constants;
import com.quik.entity.BizBilling;
import com.quik.entity.BizBillingChessRoom;
import com.quik.entity.BizBillingItem;
import com.quik.entity.BizBillingRelatedItem;
import com.quik.entity.BookingRoom;
import com.quik.entity.DtaProduct;
import com.quik.entity.Room;
import com.quik.entity.SysSystemCode;
import com.quik.entity.DtaToken;
import com.quik.util.Tools;
import com.quik.web.base.mapper.PubSystemDao;
import com.quik.web.front.mapper.ConsultBillingDao;
import com.quik.web.front.mapper.ConsultRoomDao;

/**
 * 功能说明：
 *        接口ConsultRoomService实现类
 * @author jenny
 *
 */
@Service
public class ConsultRoomServiceImpl extends ConsultBaseService implements ConsultRoomService {
	
	@Autowired
	private ConsultRoomDao dao;
	
	@Autowired
	private ConsultBillingDao billingDao;
	
	@Autowired
	private PubSystemDao systemDao;
	
	/**
	 * 查询房间类型列表
	 * @return
	 * @throws Exception
	 */
	@Override
	public List findRoomTypeList() throws Exception{
		List list = dao.findRoomTypeList();
		return list;
	}
	
	/**
	 * 查询房间区域列表
	 * @return
	 * @throws Exception
	 */
	@Override
	public List findRoomAreaList() throws Exception{
		List list = dao.findRoomAreaList();
		return list;
	}
	
	/**
	 * 获取房间状态列表
	 * @return
	 * @throws Exception
	 */
	@Override
	public List findRoomStatusList() throws Exception{
		List list = systemDao.findSystemCodeBySection(Tools.getSection(Room.TABLE_NAME, 
						Constants.SYSTEM_CODE_ROOMSTATUS));
		return list;
	}
	
	/**
	 * 根据条件查询房间列表
	 * @param roomType 房间类型ID
	 * @param roomArea  房间区域ID
	 * @param status  房间状态
	 * @return
	 * @throws Exception
	 */
	@Override
	public List findRoomList(Integer roomType, Integer roomArea, String status) throws Exception{
		List roomList = dao.findRoomList(roomType, roomArea, status, Tools.getSection(Room.TABLE_NAME, 
				              Constants.SYSTEM_CODE_ROOMSTATUS));
		return roomList;
	}
	
	/**
	 * 查询房间状态统计
	 * @return
	 * @throws Exception
	 */
	@Override
	public List findRoomStatList() throws Exception{
		List list = dao.findRoomStatList(Tools.getSection(Room.TABLE_NAME, Constants.SYSTEM_CODE_ROOMSTATUS));
		return Tools.statList(list);
	}
	
	/**
	 * 根据房间ID查询房间
	 * @param roomId
	 * @return
	 * @throws Exception
	 */
	@Override
	public Room findRoomById(Integer roomId) throws Exception{
		Room room = dao.findRoomById(roomId, Tools.getSection(Room.TABLE_NAME, Constants.SYSTEM_CODE_ROOMSTATUS));
		return room;
	}
	
	/**
	 * 根据房间ID查询绑定的商品信息
	 * @param roomId
	 * @return
	 * @throws Exception
	 */
	@Override
	public DtaProduct findProductByRoomId(Integer roomId) throws Exception{
		DtaProduct product = dao.findProductByRoomId(roomId);
		return product;
	}
	
	/**
	 * 手牌开房间
	 * 	  1.查询billing_id, 2.新增biz_billing_related_item 3.新增biz_billing_item, 4.更新房间状态
	 * @param tokenNo
	 * @param roomId
	 * @param staff_no
	 * @param qty
	 * @throws Exception
	 */
	public void openRoom(String tokenNo, String roomNo, String productNo, String service, String chess, Integer qty, String staff_no) throws Exception{
		//1.查询billing_id
		Integer billingId = billingDao.findBillingIdByItemNo(tokenNo, BizBillingRelatedItem.ITEM_TYPE_TOKEN);
		
		//2.新增biz_billing_related_item
		BizBillingRelatedItem bizBillingRelatedItem = new BizBillingRelatedItem();
		bizBillingRelatedItem.setBilling_id(billingId);
		bizBillingRelatedItem.setCrt_user(staff_no);
		bizBillingRelatedItem.setItem_no(roomNo);
		bizBillingRelatedItem.setItem_type(BizBillingRelatedItem.ITEM_TYPE_ROOM);
		billingDao.addBizBillingRelatedItem(bizBillingRelatedItem);
		
		//3.如果房间绑定商品，则新增biz_billing_item
		DtaProduct product = dao.findRoomBoundProduct(roomNo);
		if(product != null && product.getProduct_no().length() > 0){
			BizBillingItem billingItem = new BizBillingItem();
			billingItem.setBilling_id(billingId);
			billingItem.setProduct_no(product.getProduct_no());
			billingItem.setQty(qty);
			billingItem.setRoom_no(roomNo);
			billingItem.setIs_free(BizBillingItem.STATUS_NO);
			billingItem.setIs_subtract(BizBillingItem.STATUS_NO);			
			billingItem.setPrice(product.getPrice());
			billingItem.setCrt_user(staff_no);
			billingItem.setStatus(BizBillingItem.STATUS_ORDER);
			
			if(service.equals(DtaProduct.OPTION_FOR_YES)){
				int count = billingItem.getQty();
				billingItem.setQty(1);			
				for(int i = 0; i < count; i++){
					billingItem.setId(null);
					billingDao.addBizBillingItem(billingItem);
					billingDao.addBizBillingService(getBizBillingService(billingItem.getId(), staff_no));
				}
			}else{
				billingDao.addBizBillingItem(billingItem);
				
				if(chess.equals(DtaProduct.OPTION_FOR_YES)){
					List systemList = systemDao.findSystemCodeBySection(
							Tools.getSection(BizBillingChessRoom.TABLE_NAME, Constants.SYSTEM_CODE_CALCMODE));
					SysSystemCode systemCode = (SysSystemCode)systemList.get(0);
					billingDao.addBizBillingChessRoom(getBizBillingChessRoom(billingItem.getId(), 
							Integer.parseInt(systemCode.getName()), staff_no));
				}
			}
		}
		
		//4.更新房间状态
		dao.updateRoomStatus(roomNo, Room.ROOM_STATUS_USED, staff_no);
	}
	
	/**
	 * 无手牌开房间
	 *    1. 更新biz_billing；2.更新biz_billing_related_item；
	 *    3.更新biz_billing_item（当是服务类型时，则按照数量生成相同的条数的记录）
	 *    4.更新房间状态
	 * @param roomNo
	 * @param productNo
	 * @param service
	 * @param chess
	 * @param staffNo
	 * @param qty
	 * @throws Exception
	 */
	@Override
	public void openRoomNoToken(String contactStaffNo, String roomNo, String productNo, String service, String chess, Integer qty, String staffNo) throws Exception{
		String billingNo = Tools.generateBillingNo(systemDao.findBillingNo()); //获得帐单编号
		BizBilling bizBilling = new BizBilling();
		bizBilling.setBilling_no(billingNo);
		bizBilling.setContact_staff_no(contactStaffNo);
		bizBilling.setCrt_user(staffNo);
		bizBilling.setIf_master(BizBilling.MASTER_YES);		
	    Integer billingId = billingDao.addBizBilling(bizBilling);
	    
	    BizBillingRelatedItem relatedItem = new BizBillingRelatedItem();
		relatedItem.setBilling_id(billingId);
		relatedItem.setItem_no(roomNo);
		relatedItem.setItem_type(BizBillingRelatedItem.ITEM_TYPE_ROOM);
		relatedItem.setCrt_user(staffNo);
		billingDao.addBizBillingRelatedItem(relatedItem);
		
		DtaProduct product = dao.findRoomBoundProduct(roomNo);
		if(product != null && product.getProduct_no().length() > 0){
			BizBillingItem item = new BizBillingItem();
			item.setBilling_id(billingId);
			item.setProduct_no(product.getProduct_no());
			item.setQty(qty);
			item.setRoom_no(roomNo);
			item.setIs_free(BizBillingItem.STATUS_NO);
			item.setIs_subtract(BizBillingItem.STATUS_NO);			
			item.setPrice(product.getPrice());
			item.setCrt_user(staffNo);
			item.setContact_staff_no(contactStaffNo);
			
			if(service.equals(DtaProduct.OPTION_FOR_YES)){
				int count = item.getQty();
				item.setQty(1);			
				for(int i = 0; i < count; i++){
					item.setId(null);
					billingDao.addBizBillingItem(item);
					billingDao.addBizBillingService(getBizBillingService(item.getId(), staffNo));
				}
			}else{
				billingDao.addBizBillingItem(item);
				
				if(chess.equals(DtaProduct.OPTION_FOR_YES)){
					List systemList = systemDao.findSystemCodeBySection(
							Tools.getSection(BizBillingChessRoom.TABLE_NAME, Constants.SYSTEM_CODE_CALCMODE));
					SysSystemCode systemCode = (SysSystemCode)systemList.get(0);
					billingDao.addBizBillingChessRoom(getBizBillingChessRoom(item.getId(), 
							Integer.parseInt(systemCode.getName()), staffNo));
				}
			}
		}
		
		dao.updateRoomStatus(roomNo, Room.ROOM_STATUS_USED, staffNo);
	}
	
	/**
	 * 判断是否满足交换房间的条件
	 * 思路：判断旧房间号是否开房，新房间号是否未开房
	 * @param srcRoom
	 * @param newRoom
	 * @return 以上两个条件满足返回true，否则返回false
	 * @throws Exception
	 */
	@Override
	public boolean isExchanged(String srcRoom, String newRoom) throws Exception{
		 Room room = dao.findRoomByRoomNo(srcRoom);
		 if(room == null || room.getRoom_status().equals(Room.ROOM_STATUS_FREE))
			 return false;
		
		 Room nRoom = dao.findRoomByRoomNo(newRoom);
		 if(nRoom != null && nRoom.getRoom_status().equals(Room.ROOM_STATUS_FREE))
			 return true;
		 else
			 return false;
	}
	
	/**
	 * 交换房间
	 * 1.更新biz_billing_related_item 2.更新biz_billing_item 3.更新dta_room状态
	 * @param srcRoom
	 * @param newRoom
	 * @param staffNo
	 * @throws Exception
	 */
	public void exchangeRoom(String srcRoom, String newRoom, String staffNo) throws Exception{
		billingDao.updateBizbillingRelatedItemNo(srcRoom, newRoom, BizBillingRelatedItem.ITEM_TYPE_ROOM,
				BizBilling.STATUS_ACTIVE, staffNo);
		
		billingDao.updateBizBillingItemRoomNo(srcRoom, newRoom, BizBilling.STATUS_ACTIVE, staffNo);
		
		//更新源手牌状态为空闲
		dao.updateRoomStatus(srcRoom, Room.ROOM_STATUS_FREE, staffNo);
		//更新新手牌状态为占用
		dao.updateRoomStatus(newRoom, Room.ROOM_STATUS_USED, staffNo);
	}
	
	/**
	 * 根据条件查询房间数量
	 * @param roomNo
	 * @param roomType
	 * @param roomArea
	 * @param status
	 * @return
	 * @throws Exception
	 */
	@Override
	public int findRoomCount(String roomNo, Integer roomType, Integer roomArea, String status) throws Exception{
		int count = dao.findRoomCount(roomNo, roomType, roomArea, status);
		return count;
	}
	
	/**
	 * 根据条件查询房间列表（分页）
	 * @param roomNo
	 * @param roomType
	 * @param roomArea
	 * @param status
	 * @param intPage
	 * @param pageSize
	 * @return
	 * @throws Exception
	 */
	@Override
	public List findRoomList(String roomNo, Integer roomType, Integer roomArea, String status, Integer intPage, Integer pageSize) throws Exception{
		List list = dao.findRoomList(roomNo, roomType, roomArea, status, intPage, pageSize);
		return list;
	}
	
	/**
	 * 判断房间是否存在及已开房
	 * @param roomNo
	 * @return
	 * @throws Exception
	 */
	@Override
	public boolean isRoom(String roomNo) throws Exception{
		Room room = dao.findRoomByRoomNo(roomNo);
		if(room != null && room.getRoom_status().equals(Room.ROOM_STATUS_USED))
			return true;
		else
			return false;
	}
	
	/**
	 * 关联房间
	 * @param boundRoom
	 * @param relateRoom
	 * @param staffNo
	 * @throws Exception
	 */
	@Override
	public void relateByRoom(String boundRoom, String[] relateRoom, String staffNo) throws Exception{
		Integer billingId = billingDao.findBillingIdByItemNo(boundRoom, BizBillingRelatedItem.ITEM_TYPE_ROOM);
		
		for(String roomNo : relateRoom){
			BizBillingRelatedItem bizBillingRelatedItem = new BizBillingRelatedItem();
			bizBillingRelatedItem.setBilling_id(billingId);
			bizBillingRelatedItem.setCrt_user(staffNo);
			bizBillingRelatedItem.setItem_no(roomNo);
			bizBillingRelatedItem.setItem_type(BizBillingRelatedItem.ITEM_TYPE_ROOM);
			billingDao.addBizBillingRelatedItem(bizBillingRelatedItem);
			
			//3.如果房间绑定商品，则新增biz_billing_item
			DtaProduct product = dao.findRoomBoundProduct(roomNo);
			if(product != null && product.getProduct_no().length() > 0){
				BizBillingItem billingItem = new BizBillingItem();
				billingItem.setBilling_id(billingId);
				billingItem.setProduct_no(product.getProduct_no());
				billingItem.setQty(1);
				billingItem.setRoom_no(roomNo);
				billingItem.setIs_free(BizBillingItem.STATUS_NO);
				billingItem.setIs_subtract(BizBillingItem.STATUS_NO);			
				billingItem.setPrice(product.getPrice());
				billingItem.setCrt_user(staffNo);
				billingItem.setStatus(BizBillingItem.STATUS_ORDER);
				billingDao.addBizBillingItem(billingItem);
				
				if(product.getIf_chess_room().equals(DtaProduct.OPTION_FOR_YES)){
					List systemList = systemDao.findSystemCodeBySection(
							Tools.getSection(BizBillingChessRoom.TABLE_NAME, Constants.SYSTEM_CODE_CALCMODE));
					SysSystemCode systemCode = (SysSystemCode)systemList.get(0);
					billingDao.addBizBillingChessRoom(getBizBillingChessRoom(billingItem.getId(), 
							Integer.parseInt(systemCode.getName()), staffNo));
				}
				if(product.getIf_need_worker().equals(DtaProduct.OPTION_FOR_YES)){
					billingDao.addBizBillingService(getBizBillingService(billingItem.getId(), staffNo));
				}
	
			}
			
			//4.更新房间状态
			dao.updateRoomStatus(roomNo, Room.ROOM_STATUS_USED, staffNo);
		}
	}
	
	/**
	 * 判断是否可以预订（规则：确认同一天不允许预订两次）
	 * @param roomId
	 * @param bookingDate
	 * @return
	 * @throws Exception
	 */
	@Override
	public boolean isBook(Integer roomId, String bookingDate) throws Exception{
		boolean flag = true;
		int count = dao.findBookingRoomCount(roomId, bookingDate);
		if(count > 0)
			flag = false;
		return flag;
	}
	
	/**
	 * 预订房间
	 * @param roomId
	 * @param userName
	 * @param userMobile
	 * @param bookingDate
	 * @param staffNo
	 * @throws Exception
	 */
	@Override
	public void bookingRoom(Integer roomId, String userName, String userMobile, String bookingDate, String staffNo) throws Exception{
		BookingRoom bookingRoom = new BookingRoom();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		bookingRoom.setBooking_date(sdf.parse(bookingDate));
		bookingRoom.setUser_mobile(userMobile);
		bookingRoom.setUser_name(userName);
		bookingRoom.setBooking_room_id(roomId);
		bookingRoom.setCrt_user(staffNo);
		
		dao.addBookingRoom(bookingRoom);
	}
}
